<?php
    class Media extends FrontEnd_Controller{
        function Media(){
            parent::FrontEnd_Controller();
            $this->load->model('Media_m');
            $this->load->helper('date');
        }
        function liste_media(){
            $per_page = $this->uri->segment(3)!=''?$this->uri->segment(3):12;
            $off_set = $this->uri->segment(4)!=''?$this->uri->segment(4):0;
            $config['base_url'] = base_url() . 'index.php/media/liste_media/' . $per_page . '/';
            $nav_link = $this->session->set_userdata('smart_navigate_url',  site_url('media/liste_media'));
            $this->session->set_userdata('smart_navigate_url',$config['base_url'].$off_set);
            $config['per_page'] = $per_page;
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->db->count_all('seating_videos');
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['videos'] = $this->Media_m->get_all_videos($per_page,$off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->display_view('media/liste_de_video');
        }
        function media_play($vid_id){
            $per_page = $this->uri->segment(4);
            if ($per_page == "") {
                $per_page = 10;
            }
            $off_set = $this->uri->segment(5);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/media/media_play/'.$vid_id . '/' . $per_page . '/';
            $this->session->set_userdata('smart_navigate_url',$config['base_url'].$off_set);
            $config['per_page'] = '10';
            $config['uri_segment'] = 5;
            $config['total_rows'] = $this->Media_m->count_comment_videos($vid_id);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['video_comment'] = $this->Media_m->get_comment_videos($vid_id,$config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Media_m->count_comment_videos($vid_id);
            $this->_data['page_title'] = "VIDEOS";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;

            $this->_data['video_to_play']=$this->Media_m->get_video_details($vid_id);
            $this->display_view('media/media_player');
        }

        function comment_video($vid_id){
            if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'comment',
                    'Label' => 'Comment',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);

                if ($this->form_validation->run() == TRUE) {
                        $data = array(
                            'comment' => $this->input->post('comment'),
                            'comment_date' => now(),
                            'video_id' => $vid_id,
                            'user_id'=>$this->session->userdata('user_id')
                        );
                        $this->Media_m->comment_video($data);
                        $this->media_play($vid_id);
                } else {
                    $this->media_play($vid_id);
                }
            } else {
                $this->media_play($vid_id);
            }
        }
    }
?>
